package com.jn.erp.web.api.interaction.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jn.erp.web.api.common.entity.TLabel;
import com.jn.erp.web.api.common.mapper.TLabelMapper;
import com.jn.erp.web.api.course.entity.dto.TCourseOrderDto;
import com.jn.erp.web.api.course.mapper.TCourseOrderMapper;
import com.jn.erp.web.api.interaction.entity.*;
import com.jn.erp.web.api.interaction.entity.vo.TEvaluateVo;
import com.jn.erp.web.api.interaction.mapper.*;
import com.jn.erp.web.api.interaction.service.TEvaluateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wyp
 * @since 2024-09-20
 */
@Service
public class TEvaluateServiceImpl extends ServiceImpl<TEvaluateMapper, TEvaluate> implements TEvaluateService {
    @Autowired
    private TEvaluateScoreMapper tEvaluateScoreMapper;

    @Autowired
    private TEvaluateTemplateMapper tEvaluateTemplateMapper;

    @Autowired
    private TLabelMapper tLabelMapper;


    @Autowired
    private TCourseOrderMapper tCourseOrderMapper;

    @Autowired
    private TIntegralConfigMapper tIntegralConfigMapper;

    @Autowired
    private TIntegralMapper tIntegralMapper;
    @Override
    public Map count(TEvaluateVo tEvaluateVo) {
        Map map = baseMapper.count(tEvaluateVo);
        return map;
    }

    @Override
    public TEvaluate evaluateGetById(TEvaluateVo tEvaluateVo) {
        TEvaluate tEvaluate = baseMapper.selectById(tEvaluateVo.getId());
        List<TEvaluateScore> tEvaluateScores = tEvaluateScoreMapper.selectList(new QueryWrapper<TEvaluateScore>().eq("is_delete", "0").eq("e_id", tEvaluateVo.getId()));
        tEvaluate.setTEvaluateScores(tEvaluateScores);
        Long templateId = Objects.isNull(tEvaluateVo.getTemplateId())?tEvaluate.getTemplateId():tEvaluateVo.getTemplateId();
        TEvaluateTemplate tEvaluateTemplate = tEvaluateTemplateMapper.selectById(templateId);
        List<TLabel> tLabelList = tLabelMapper.selectList(new QueryWrapper<TLabel>().in("id", tEvaluateTemplate.getInfo()));
        tEvaluate.setTLabelList(tLabelList);
        return tEvaluate;

    }

    @Override
    public String updateEvaluate(TEvaluateVo tEvaluateVo) {
        TEvaluate tEvaluate = baseMapper.selectById(tEvaluateVo.getId());
        Boolean tIntegralFlag = false;
        if(1 == tEvaluate.getState() && 0 == tEvaluateVo.getState()){
            tIntegralFlag = true;
        }
        int i = baseMapper.updateById(tEvaluateVo);
        TEvaluateScore tEvaluateScore = new TEvaluateScore();
        tEvaluateScore.setIsDelete("1");
        int eId = tEvaluateScoreMapper.update(tEvaluateScore, new QueryWrapper<TEvaluateScore>().eq("e_id", tEvaluateVo.getId()));
        for (TEvaluateScore evaluateScore : tEvaluateVo.getTEvaluateScores()) {
            evaluateScore.setCreateUser(tEvaluateVo.getUpdateUser());
            evaluateScore.setCreateDate(LocalDateTime.now());
            tEvaluateScoreMapper.insert(evaluateScore);
            if(tIntegralFlag){
                //添加积分
                TIntegralConfig t = tIntegralConfigMapper.selectOne(new QueryWrapper<TIntegralConfig>().eq("org_id", tEvaluateVo.getOrgId()).eq("is_delete","0"));
                if(!Objects.isNull(t)){
                    TIntegral tIntegral = new TIntegral(tEvaluateVo.getOrgId(),tEvaluateVo.getTId(),t.getId(),3,t.getAttr3(),t.getName(),"评价",LocalDateTime.now(),String.valueOf(tEvaluateVo.getUpdateUser()) ,null,null,"0");
                    tIntegralMapper.insert(tIntegral);
                }
            }
        }

        return "S200";

    }

    @Override
    public List<TCourseOrderDto> evaluateStudentList(TEvaluateVo tEvaluateVo) {
        return tCourseOrderMapper.evaluateStudentList(tEvaluateVo);
    }
}
